<?php
/*======================================================================*\
|| #################################################################### ||
|| # ragtek Template Compare  1.0.0
|| # Build: 1
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2011 Daniel Fatkic aka ragtek. All Rights Reserved.   # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # http://www.ragtek.org                                            # ||
|| #################################################################### ||
\*======================================================================*/


class Ragtek_TC_Model_TemplateCompare extends XenForo_Model {

    public function getTemplates($templateName) {
        $query = "SELECT template.template_id,
                 template.title, template.style_id,
                 template.template,
                 style.title AS styletitle
            FROM xf_template AS template
            left JOIN xf_style AS style ON (style.style_id = template.style_id)
            where template.title =  ?";

        return $this->_getDb()->fetchAll($query, $templateName);
    }

    public function diff(array $old, array $new, $style1 = 'master' , $style2 = '', $renderer = 'Ragtek_Helper_Diff_Renderer') {
        $diff = new Ragtek_Helper_Diff($old, $new);

        $renderer = new $renderer();

        $out = $diff->Render($renderer);
        $out = $this->replaceTable($out, $style1, $style2);
        return $out;
    }

    /**
     * rename the table header, until we use our own render modul for this!
     * @param  $content
     * @param  $val1
     * @param  $val2
     * @return mixed
     */
    protected function replaceTable($content, $val1, $val2){
        $search = array(
            '<th colspan="2">Old Version</th>',
            '<th colspan="2">New Version</th>'
        );
        $replace = array(
            '<th colspan="2">' . $val1 . '</th>',
            '<th colspan="2">' . $val2 . '</th>'
        );

       return str_replace($search, $replace, $content);
    
    }
}